package com.hoshiicloud.market.service.impl.activities;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.entity.activities.ActivitiesGoods;
import com.hoshiicloud.market.feign.IdGeneratorService;
import com.hoshiicloud.market.mapper.activities.ActivitiesGoodsMapper;
import com.hoshiicloud.market.service.activities.ActivitiesGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author:campchen
 * @Date:Created in 下午 04:40 2019/8/26
 * @PACKAGENAME com.hoshiicloud.market.service.impl.activities
 * @Description
 */
@Service
public class ActivitiesGoodsServiceImpl extends ServiceImpl<ActivitiesGoodsMapper,ActivitiesGoods> implements ActivitiesGoodsService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Cacheable(cacheNames = "ActivitiesGoods", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public ActivitiesGoods getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ActivitiesGoods", allEntries = true)
    @Override
    public String insert(ActivitiesGoods entity) {
        String id=idGeneratorService.queryGeneratorId();
        if (id==null||"".equals(id)){
            throw new ServiceException(CommonError.error("id生成有误"));
        }
        entity.setCreatedTime(LocalDateTime.now());
        entity.setId(Long.parseLong(id));
        int row=baseMapper.insert(entity);
        if (row<0){
            throw new ServiceException(CommonError.error("新增错误"));
        }
        return id;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "ActivitiesGoods", allEntries = true)
    @Override
    public boolean updateById(ActivitiesGoods entity) {
        entity.setModifiedTime(LocalDateTime.now());
        int row=baseMapper.updateById(entity);
        if (row<0){
            throw new ServiceException(CommonError.error("修改错误"));
        }
        return true;
    }

    @Cacheable(cacheNames = "ActivitiesGoods", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public IPage<ActivitiesGoods> getPage(IPage<ActivitiesGoods> page, QueryWrapper<ActivitiesGoods> queryWrapper, String key) {
        return baseMapper.selectPage(page,queryWrapper);
    }
}
